import { streamText } from 'ai'
import { createOpenAI } from '@ai-sdk/openai'

export const runtime = 'edge' // 更低延迟

const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY })

export async function POST(req: Request) {
  const { messages, mode, tone } = await req.json()

  const system = `You are a concise, practical writing assistant.\n` +
    `- Mode: ${mode ?? 'general'} (rewrite/expand/summarize/translate/general)\n` +
    `- Tone: ${tone ?? 'neutral'}\n` +
    `- Output clean markdown. Avoid preambles.`

  const result = await streamText({
    model: openai('gpt-4o-mini'),
    system,
    messages,
  })

  return result.toAIStreamResponse()
}